/**
 * 
 */
package de.damontools.AWoLoWA;

/**
 * @author damon
 *
 */
//import java.io.*;
import java.io.IOException;
import java.net.*;

public class WakeOnLan {
    
    public static final int PORT = 9;    
  public static void wakeup(String macStr){
    	String ipStr = "255.255.255.255";
        try {
            byte[] macBytes = getMacBytes(macStr);
            byte[] bytes = new byte[6 + 16 * macBytes.length];
            for (int i = 0; i < 6; i++) {
                bytes[i] = (byte) 0xff;
            }
            for (int i = 6; i < bytes.length; i += macBytes.length) {
                System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
            }
            
            InetAddress address = InetAddress.getByName(ipStr);
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
            DatagramSocket socket = new DatagramSocket();
            socket.send(packet);
            socket.close();
            WiFiCheck.setTxtStatus("Sent WoL-packet to: " + macStr);
            
        }
        catch (Exception e) {
            System.exit(1);
        }
        
    }
 
    public static void wakeup(String ipStr, byte[] mac) throws IOException {
		//
		// WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence
		// containing the MAC address.
		//
		byte[] packet = new byte[17 * 6];

		//
		// Trailer of 6 times 0xFF.
		//
		for (int i = 0; i < 6; i++)
			packet[i] = (byte) 0xFF;

		//
		// Body of magic packet contains 16 times the MAC address.
		//
		for (int i = 1; i <= 16; i++)
			for (int j = 0; j < 6; j++)
				packet[i * 6 + j] = mac[j];

		//
		// Submit WOL packet.
		//

		InetAddress address = InetAddress.getByName(ipStr);
		DatagramPacket packetToSend = new DatagramPacket(packet, packet.length, address, PORT);
		DatagramSocket socket = new DatagramSocket();
		socket.send(packetToSend);
		socket.close();
	}
   public static void wakeup(String ipStr, String macStr){
    	
        try {
            byte[] macBytes = getMacBytes(macStr);
            byte[] bytes = new byte[6 + 16 * macBytes.length];
            for (int i = 0; i < 6; i++) {
                bytes[i] = (byte) 0xff;
            }
            for (int i = 6; i < bytes.length; i += macBytes.length) {
                System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
            }
            
            InetAddress address = InetAddress.getByName(ipStr);
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
            DatagramSocket socket = new DatagramSocket();
            socket.send(packet);
            socket.close();
            
        }
        catch (Exception e) {
            System.exit(1);
        }
        
    }
    private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
        byte[] bytes = new byte[6];
        String[] hex = macStr.split("(\\:|\\-)");
        if (hex.length != 6) {
            throw new IllegalArgumentException("Invalid MAC address.");
        }
        try {
            for (int i = 0; i < 6; i++) {
                bytes[i] = (byte) Integer.parseInt(hex[i], 16);
            }
        }
        catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid hex digit in MAC address.");
        }
        return bytes;
    }
    
     
}